import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/5/11
 * \* Time: 21:37
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 二叉树中第二小的节点 {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
    }
    public int findSecondMinimumValue(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        PreOrder(root,list);
        int[] arr=new int[list.size()];
        for(int i=0;i<arr.length;i++){
            arr[i]=list.get(i);
        }
        Arrays.sort(arr);
        if(arr.length<=1){
            return -1;
        }
        for(int i=1;i<arr.length;i++){
            if(arr[i]>arr[i-1]){
                return arr[i];
            }
        }
        return -1;
    }
    public void PreOrder(TreeNode root,List<Integer> list){
        if(root==null){
            return;
        }
        list.add(root.val);
        PreOrder(root.left,list);
        PreOrder(root.right,list);
    }
}